

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[TOC] 一、运行环境  windows10 IDEA 2022 JDK 8 Maven 3.8.6 Apache POI 5 fastjson2  二、需求描述 写一个功能，任意json生成excel，每个数组都单独生成一个sheet。 三、实现思路 参考资料：Apache POI 使用教程 主要实现思路： 使用支持Java对象与JSON对象、字符串互相转换的fastjson，以及支持Jav">
<meta property="og:type" content="article">
<meta property="og:title" content="Java实现将JSON文件导出到Excel">
<meta property="og:url" content="http://example.com/2022/07/06/Java%E5%AE%9E%E7%8E%B0%E5%B0%86JSON%E6%96%87%E4%BB%B6%E5%AF%BC%E5%87%BA%E5%88%B0Excel/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[TOC] 一、运行环境  windows10 IDEA 2022 JDK 8 Maven 3.8.6 Apache POI 5 fastjson2  二、需求描述 写一个功能，任意json生成excel，每个数组都单独生成一个sheet。 三、实现思路 参考资料：Apache POI 使用教程 主要实现思路： 使用支持Java对象与JSON对象、字符串互相转换的fastjson，以及支持Jav">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/b1bfab99aac14fa899931382cd716e4f.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/0ffd3aa5539f439d8b3d146033817f6f.png">
<meta property="article:published_time" content="2022-07-06T07:29:12.000Z">
<meta property="article:modified_time" content="2022-08-22T15:45:18.435Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/b1bfab99aac14fa899931382cd716e4f.png">
  
  
  
  <title>Java实现将JSON文件导出到Excel - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Java实现将JSON文件导出到Excel"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-07-06 15:29" pubdate>
          2022年7月6日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          14k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          117 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Java实现将JSON文件导出到Excel</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[TOC]</p>
<h1 id="一、运行环境"><a href="#一、运行环境" class="headerlink" title="一、运行环境"></a>一、运行环境</h1><hr>
<ul>
<li>windows10</li>
<li>IDEA 2022</li>
<li>JDK 8</li>
<li>Maven 3.8.6</li>
<li>Apache POI 5</li>
<li>fastjson2</li>
</ul>
<h1 id="二、需求描述"><a href="#二、需求描述" class="headerlink" title="二、需求描述"></a>二、需求描述</h1><hr>
<p>写一个功能，任意json生成excel，每个数组都单独生成一个sheet。</p>
<h1 id="三、实现思路"><a href="#三、实现思路" class="headerlink" title="三、实现思路"></a>三、实现思路</h1><hr>
<p>参考资料：<a target="_blank" rel="noopener" href="https://www.codingdict.com/article/8763">Apache POI 使用教程</a></p>
<p><strong>主要实现思路：</strong> 使用支持Java对象与JSON对象、字符串互相转换的fastjson，以及支持Java将JSON转化Excel的库 apache-poi</p>
<p><strong>Excel表格关键结构：</strong></p>
<ul>
<li>Workbook 工作台，相当于一个 excel文件</li>
<li>sheet，一个excel文件中的表格页面，可能有多个<ul>
<li>row，所在sheet中的行<ul>
<li>cel，所在sheet中所在行的列<ul>
<li>value，所在单元格的值</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/b1bfab99aac14fa899931382cd716e4f.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>JSON转换的几种情形与实现思路：</p>
<p><strong>情形一：普通的单层结构，多个JSON对象</strong></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-attr">&quot;班级A&quot;</span> <span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">&#123;</span><br>      <span class="hljs-attr">&quot;文章&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;课文1&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;作者&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;李白&quot;</span><br>    <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-punctuation">&#123;</span><br>      <span class="hljs-attr">&quot;文章&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;课文2&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;作者&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;小李&quot;</span><br>    <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-punctuation">&#123;</span><br>      <span class="hljs-attr">&quot;文章&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;课文2&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;作者&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小明&quot;</span><br>    <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">]</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>
<p>导出结果：</p>
<p><img src="https://img-blog.csdnimg.cn/0ffd3aa5539f439d8b3d146033817f6f.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>当我们使用fastjson遍历JSONObject时，每次读取到的都是单个{ } 所包含的对象，比如：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>  <span class="hljs-attr">&quot;文章&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;课文1&quot;</span><span class="hljs-punctuation">,</span><br>  <span class="hljs-attr">&quot;作者&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;李白&quot;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>
<p>这种情况下，我们在Excel的Sheet中的行是确定的，比如这里就是第二行（第一行是列名），行根据遍历的顺序确定，而列则是不确定的，在这里有 “文章”，”作者” 这两个列，但是一开始这两个列是不存在的。这里则确定文章在第一列，作者按第二列（默认升序排序）。</p>
<p>当遍历下一个对象时，我们可能遇到旧的列，也可能遇到新的列，比如：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>  <span class="hljs-attr">&quot;文章&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;课文2&quot;</span><span class="hljs-punctuation">,</span><br>  <span class="hljs-attr">&quot;作者&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;李白&quot;</span><span class="hljs-punctuation">,</span><br>  <span class="hljs-attr">&quot;出版日期&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;2022年7月6日&quot;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>
<p>这时，我们需要知道”文章” 和 “作者” 在第几列，同时也要知道 “出版日期” 应该在第几列，否则就不能确定唯一的单元格，将 JSON的value存储进去。</p>
<p>这里可以使用 <code>Map&lt;String, Integer&gt; map</code> 来记录列名以及下标。</p>
<p>在遍历对象时，key是列名，value则是单元格该填的值，如果 map.get(key) 的结果是空的，说明该列不存在，则需要创建，如果存在，那么可以创建单元格的对象，将值填入即可。</p>
<p><strong>情形二：嵌套结构，JSON数组的嵌套</strong></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>  <span class="hljs-attr">&quot;班级A&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-punctuation">[</span><br>    <span class="hljs-punctuation">&#123;</span><br>      <span class="hljs-attr">&quot;学号&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;A01&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;语文&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-punctuation">[</span><br>        <span class="hljs-punctuation">&#123;</span><br>          <span class="hljs-attr">&quot;文章&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;课文1&quot;</span><span class="hljs-punctuation">,</span><br>          <span class="hljs-attr">&quot;作者&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;李白&quot;</span><br>        <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-punctuation">&#123;</span><br>          <span class="hljs-attr">&quot;文章&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;课文2&quot;</span><span class="hljs-punctuation">,</span><br>          <span class="hljs-attr">&quot;作者&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;小李&quot;</span><br>        <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-punctuation">&#123;</span><br>          <span class="hljs-attr">&quot;文章&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;课文2&quot;</span><span class="hljs-punctuation">,</span><br>          <span class="hljs-attr">&quot;作者&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小明&quot;</span><br>        <span class="hljs-punctuation">&#125;</span><br>      <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;数学&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;130&quot;</span><br>    <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-punctuation">&#123;</span><br>      <span class="hljs-attr">&quot;学号&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;A02&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;语文&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;130&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;数学&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;135&quot;</span><br>    <span class="hljs-punctuation">&#125;</span><br>  <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<p><strong>实现效果：</strong></p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/a4bfe01d291446efb888824667900309.png#">https://img-blog.csdnimg.cn/a4bfe01d291446efb888824667900309.png#</a> &#x3D;50%x)</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/c3c60f9cd9eb4f19b3c2e6c9bd2688a9.png#">https://img-blog.csdnimg.cn/c3c60f9cd9eb4f19b3c2e6c9bd2688a9.png#</a> &#x3D;50%x)</p>
<p>这里相比之前的情况复杂了一些，主要的就是需要再次创建一个新的 sheet，这意味着需要使用递归完成创建，于是我们可以将之前那种情形的代码实现封装成一个方法，比如createSubSheet()，在遍历JSON对象时，如果value值是一个JSONAarray，那么就再次调用createSubSheet()这个方法，只要使用同一个Workbook对象，表示同一个excel文件，就能满足这个需求了。</p>
<h1 id="四、实现代码"><a href="#四、实现代码" class="headerlink" title="四、实现代码"></a>四、实现代码</h1><hr>
<p><code>pom.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modelVersion</span>&gt;</span>4.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">modelVersion</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>groupId<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>poi_demo<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.source</span>&gt;</span>8<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.source</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.target</span>&gt;</span>8<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.target</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.poi<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>poi-ooxml<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.2.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.commons<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>commons-lang3<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.9<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.alibaba<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>fastjson<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.0.7<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">project</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><code>JSONToExcelUtil.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni;<br><br><span class="hljs-keyword">import</span> com.alibaba.fastjson2.JSON;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson2.JSONArray;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson2.JSONObject;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson2.JSONWriter;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson2.schema.JSONSchema;<br><span class="hljs-keyword">import</span> org.apache.commons.io.FileUtils;<br><span class="hljs-keyword">import</span> org.apache.commons.lang3.StringUtils;<br><span class="hljs-keyword">import</span> org.apache.poi.ss.usermodel.Workbook;<br><span class="hljs-keyword">import</span> org.apache.poi.xssf.usermodel.XSSFCell;<br><span class="hljs-keyword">import</span> org.apache.poi.xssf.usermodel.XSSFRow;<br><span class="hljs-keyword">import</span> org.apache.poi.xssf.usermodel.XSSFSheet;<br><span class="hljs-keyword">import</span> org.apache.poi.xssf.usermodel.XSSFWorkbook;<br><br><span class="hljs-keyword">import</span> java.io.*;<br><span class="hljs-keyword">import</span> java.util.HashMap;<br><span class="hljs-keyword">import</span> java.util.Map;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> *  uni</span><br><span class="hljs-comment"> *  2022/07/05~2022/07/06</span><br><span class="hljs-comment"> *  将 JSON 转化为 Excel的工具类</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">JSONToExcelUtil</span> &#123;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 读取绝对路径下的json文件</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> resourcePath json文件的绝对路径</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> json文件格式化后的字符串</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">readJSONFile</span><span class="hljs-params">(String resourcePath)</span> &#123;<br>        <span class="hljs-keyword">try</span>&#123;<br>            <span class="hljs-comment">// 1. 创建文件流</span><br>            <span class="hljs-type">File</span> <span class="hljs-variable">file</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">File</span>(resourcePath);<br>            <span class="hljs-comment">// 2. 使用 common-lang3工具包, 以 UTF-8 格式读取文件, 转为字符串</span><br>            <span class="hljs-type">String</span> <span class="hljs-variable">str</span> <span class="hljs-operator">=</span> FileUtils.readFileToString(file, <span class="hljs-string">&quot;UTF-8&quot;</span>);<br>            <span class="hljs-type">JSONObject</span> <span class="hljs-variable">jsonObject</span> <span class="hljs-operator">=</span> JSONObject.parseObject(str);<br>            <span class="hljs-comment">// 3. 将字符串转为标准的JSON格式的字符串</span><br>            <span class="hljs-keyword">return</span> JSONObject.toJSONString(jsonObject, JSONWriter.Feature.WriteMapNullValue);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 创建 Sheet</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> layer 当前Sheet所在JSON中的层级</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> workbook 工作台 ( excel表格的主体 )</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> sheetName 当前页的名称</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> jsonArray JSON数组</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">createSubSheet</span><span class="hljs-params">(<span class="hljs-type">int</span> layer, XSSFWorkbook workbook, String sheetName, JSONArray jsonArray)</span>&#123;<br>        <span class="hljs-comment">// 创建新的 sheet</span><br>        <span class="hljs-type">XSSFSheet</span> <span class="hljs-variable">sheet</span> <span class="hljs-operator">=</span> workbook.createSheet(sheetName);<br>        <span class="hljs-comment">// 存储每个字段</span><br>        Map&lt;String, Integer&gt; map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>        <span class="hljs-comment">// 统计当前的列</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">cellCount</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-comment">// 创建第一行</span><br>        <span class="hljs-type">XSSFRow</span> <span class="hljs-variable">firstRow</span> <span class="hljs-operator">=</span> sheet.createRow(<span class="hljs-number">0</span>);<br>        <span class="hljs-comment">// 获取每一项</span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">row</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; row &lt;= jsonArray.size(); row++) &#123;<br>            <span class="hljs-type">JSONObject</span> <span class="hljs-variable">jsonObject</span> <span class="hljs-operator">=</span> jsonArray.getJSONObject(row - <span class="hljs-number">1</span>);<br>            <span class="hljs-comment">// 创建行</span><br>            <span class="hljs-type">XSSFRow</span> <span class="hljs-variable">currentRow</span> <span class="hljs-operator">=</span> sheet.createRow(row);<br>            <span class="hljs-keyword">if</span>(jsonObject != <span class="hljs-literal">null</span>)&#123;<br>                <span class="hljs-comment">// 遍历每个KV</span><br>                <span class="hljs-keyword">for</span> (String cellName : jsonObject.keySet()) &#123;<br>                    <span class="hljs-comment">// 列不存在时, 则创建列</span><br>                    <span class="hljs-keyword">if</span> (!map.containsKey(cellName)) &#123;<br>                        <span class="hljs-comment">// 第一行创建列</span><br>                        <span class="hljs-type">XSSFCell</span> <span class="hljs-variable">firstRowCell</span> <span class="hljs-operator">=</span> firstRow.createCell(cellCount);<br>                        firstRowCell.setCellValue(cellName);<br>                        map.put(cellName, cellCount++);<br>                    &#125;<br>                    <span class="hljs-comment">// 设置单元格</span><br>                    <span class="hljs-type">XSSFCell</span> <span class="hljs-variable">cell</span> <span class="hljs-operator">=</span> currentRow.createCell(map.get(cellName));<br>                    <span class="hljs-comment">// 获取 Value</span><br>                    <span class="hljs-type">String</span> <span class="hljs-variable">cellValue</span> <span class="hljs-operator">=</span> JSON.toJSONString(jsonObject.get(cellName));<br><br>                    <span class="hljs-comment">// 如果V为数组则递归创建sheet</span><br>                    <span class="hljs-keyword">if</span>(JSON.isValidArray(cellValue))&#123;<br>                        <span class="hljs-type">String</span> <span class="hljs-variable">subCellName</span> <span class="hljs-operator">=</span> sheetName + <span class="hljs-string">&quot;-&quot;</span> + cellName;<br>                        cell.setCellValue(subCellName);<br>                        createSubSheet(layer + <span class="hljs-number">1</span>, workbook,subCellName, jsonObject.getJSONArray(cellName));<br>                    &#125;<br>                    <span class="hljs-keyword">else</span>&#123;<br>                        cell.setCellValue(jsonObject.getString(cellName));<br>                    &#125;<br>                &#125;<br>            &#125; <span class="hljs-keyword">else</span>&#123; <span class="hljs-comment">// Value为一个数组</span><br>                <span class="hljs-type">JSONArray</span> <span class="hljs-variable">array</span> <span class="hljs-operator">=</span> jsonArray.getJSONArray(row - <span class="hljs-number">1</span>);<br>                <span class="hljs-comment">// 遍历数组</span><br>                <span class="hljs-keyword">if</span>(array != <span class="hljs-literal">null</span> &amp;&amp; array.size() &gt; <span class="hljs-number">0</span>)&#123;<br>                    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i &lt;= array.size(); i++) &#123;<br>                        <span class="hljs-type">JSONObject</span> <span class="hljs-variable">obj</span> <span class="hljs-operator">=</span> array.getJSONObject(i - <span class="hljs-number">1</span>);<br>                        <span class="hljs-comment">// 遍历 obj</span><br>                        <span class="hljs-keyword">for</span> (String cellName : obj.keySet()) &#123;<br>                            <span class="hljs-comment">// 若列不存在则添加</span><br>                            <span class="hljs-keyword">if</span>(!map.containsKey(cellName))&#123;<br>                                <span class="hljs-type">XSSFCell</span> <span class="hljs-variable">cell</span> <span class="hljs-operator">=</span> firstRow.createCell(cellCount);<br>                                map.put(cellName, cellCount++);<br>                                cell.setCellValue(cellName);<br>                            &#125;<br>                            <span class="hljs-comment">// 分情况讨论</span><br>                            <span class="hljs-type">String</span> <span class="hljs-variable">cellValue</span> <span class="hljs-operator">=</span> obj.getString(cellName);<br>                            <span class="hljs-type">XSSFCell</span> <span class="hljs-variable">cell</span> <span class="hljs-operator">=</span> currentRow.createCell(map.get(cellName));<br>                            <span class="hljs-comment">// 如果值是JSON对象, 则递归创建</span><br>                            <span class="hljs-keyword">if</span>(JSON.isValidObject(cellValue))&#123;<br>                                <span class="hljs-type">String</span> <span class="hljs-variable">subSheetName</span> <span class="hljs-operator">=</span> sheetName + <span class="hljs-string">&quot;-&quot;</span> + cellName;<br>                                cell.setCellValue(subSheetName);<br>                                createSubSheet(layer+<span class="hljs-number">1</span>, workbook, subSheetName , JSONObject.parseObject(cellValue));<br>                            &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(JSON.isValidArray(cellValue))&#123;<br>                                <span class="hljs-type">String</span> <span class="hljs-variable">subSheetName</span> <span class="hljs-operator">=</span> sheetName + <span class="hljs-string">&quot;-&quot;</span> + cellName;<br>                                cell.setCellValue(subSheetName);<br>                                createSubSheet(layer+<span class="hljs-number">1</span>, workbook, subSheetName , JSONArray.parseArray(cellValue));<br>                            &#125; <span class="hljs-keyword">else</span> &#123;<br>                                cell.setCellValue(cellValue);<br>                            &#125;<br>                        &#125;<br>                    &#125;<br>                &#125; <span class="hljs-keyword">else</span> &#123;<br>                    firstRow.createCell(<span class="hljs-number">0</span>).setCellValue(sheetName);<br>                    <span class="hljs-type">XSSFCell</span> <span class="hljs-variable">cell</span> <span class="hljs-operator">=</span> currentRow.createCell(cellCount);<br>                    cell.setCellValue(jsonArray.getString(row-<span class="hljs-number">1</span>));<br>                &#125;<br>            &#125;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 创建 Sheet</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> layer 当前Sheet所在JSON中的层级</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> workbook 工作台 ( excel表格的主体 )</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> sheetName 当前页的名称</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> jsonObject JSON对象</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">createSubSheet</span><span class="hljs-params">(<span class="hljs-type">int</span> layer, XSSFWorkbook workbook, String sheetName, JSONObject jsonObject)</span>&#123;<br>        <span class="hljs-comment">// 创建新的 sheet</span><br>        <span class="hljs-type">XSSFSheet</span> <span class="hljs-variable">sheet</span> <span class="hljs-operator">=</span> workbook.createSheet(sheetName);<br>        <span class="hljs-comment">// 存储每个字段</span><br>        Map&lt;String, Integer&gt; map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>        <span class="hljs-comment">// 统计当前的列</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">cellCount</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-comment">// 创建第一行</span><br>        <span class="hljs-type">XSSFRow</span> <span class="hljs-variable">fistRow</span> <span class="hljs-operator">=</span> sheet.createRow(<span class="hljs-number">0</span>);<br>        <span class="hljs-comment">// 记录行数</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">row</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;<br>        <span class="hljs-comment">// 获取每一项</span><br>        <span class="hljs-comment">// 创建行</span><br>        <span class="hljs-type">XSSFRow</span> <span class="hljs-variable">currentRow</span> <span class="hljs-operator">=</span> sheet.createRow(row);<br>        <span class="hljs-comment">// 遍历每个KV</span><br>        <span class="hljs-keyword">for</span> (String cellName : jsonObject.keySet()) &#123;<br>            <span class="hljs-comment">// 列不存在时, 则创建列</span><br>            <span class="hljs-keyword">if</span> (!map.containsKey(cellName)) &#123;<br>                <span class="hljs-comment">// 第一行创建列</span><br>                <span class="hljs-type">XSSFCell</span> <span class="hljs-variable">firstRowCell</span> <span class="hljs-operator">=</span> fistRow.createCell(cellCount);<br>                firstRowCell.setCellValue(cellName);<br>                map.put(cellName, cellCount++);<br>            &#125;<br>            <span class="hljs-comment">// 设置单元格</span><br>            <span class="hljs-type">XSSFCell</span> <span class="hljs-variable">cell</span> <span class="hljs-operator">=</span> currentRow.createCell(map.get(cellName));<br>            <span class="hljs-comment">// 获取 Value</span><br>            <span class="hljs-type">String</span> <span class="hljs-variable">cellValue</span> <span class="hljs-operator">=</span> JSON.toJSONString(jsonObject.get(cellName));<br>            <span class="hljs-comment">// 如果V为对象则递归创建sheet</span><br>            <span class="hljs-keyword">if</span>(JSON.isValidObject(cellValue))&#123;<br>                <span class="hljs-type">String</span> <span class="hljs-variable">subCellName</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Sheet&quot;</span> + layer + <span class="hljs-string">&quot;-&quot;</span> + sheetName + <span class="hljs-string">&quot;-&quot;</span> + cellName;<br>                cell.setCellValue(subCellName);<br>                createSubSheet(layer + <span class="hljs-number">1</span>, workbook,subCellName, JSON.parseObject(cellValue));<br><br>            &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(JSON.isValidArray(cellValue))&#123;<br>                <span class="hljs-type">String</span> <span class="hljs-variable">subCellName</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Sheet&quot;</span> + layer + <span class="hljs-string">&quot;-&quot;</span> + sheetName + <span class="hljs-string">&quot;-&quot;</span> + cellName;<br>                cell.setCellValue(subCellName);<br>                createSubSheet(layer + <span class="hljs-number">1</span>, workbook,subCellName, JSON.parseArray(cellValue));<br>            &#125;<br>            <span class="hljs-keyword">else</span>&#123;<br>                cell.setCellValue(jsonObject.getString(cellName));<br>            &#125;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 将格式化的JSON字符串导出为Excel</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> jsonStr 格式化后的JSON字符串</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> savePath Excel保存路径</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> excelName Excel名称</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">toExcelByString</span><span class="hljs-params">(String jsonStr, String savePath, String excelName)</span>&#123;<br>        <span class="hljs-keyword">assert</span> JSON.isValid(jsonStr) : <span class="hljs-string">&quot;字符串: &quot;</span> + jsonStr + <span class="hljs-string">&quot; 不是标准的JSON字符串&quot;</span>;<br>        toExcelByJSONObject(JSONObject.parseObject(jsonStr),savePath, excelName);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 将普通的Java对象导出为JSON文件</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> obj   Java对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> savePath  Excel保存路径</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> excelName Excel名称</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">toExcelByObject</span><span class="hljs-params">(Object obj, String savePath, String excelName)</span>&#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">jsonStr</span> <span class="hljs-operator">=</span> JSON.toJSONString(obj, JSONWriter.Feature.WriteMapNullValue);<br>        <span class="hljs-type">JSONObject</span> <span class="hljs-variable">jsonObject</span> <span class="hljs-operator">=</span> JSONObject.parseObject(jsonStr);<br>        toExcelByJSONObject(jsonObject, savePath, excelName);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 将本地的JSON文件导出为 Excel</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> resourcePath JSON文件的绝对路径</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> savePath  保存的路径</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> excelName 保存的Excel名称</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">toExcelByLocalJSONFile</span><span class="hljs-params">(String resourcePath, String savePath, String excelName)</span>&#123;<br>        <span class="hljs-comment">// 1. 获取标准的 JSON 字符串</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">jsonStr</span> <span class="hljs-operator">=</span> readJSONFile(resourcePath);<br>        <span class="hljs-comment">// 验证字符串是否合法</span><br>        <span class="hljs-keyword">assert</span> JSON.isValid(jsonStr) : <span class="hljs-string">&quot;路径:[&quot;</span> + resourcePath + <span class="hljs-string">&quot;] 的json文件不符合标准的JSON格式&quot;</span>;<br>        toExcelByString(jsonStr, savePath, excelName);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 将JSONObject转化导出到 Excel</span><br><span class="hljs-comment">     * 这里遵循递归导出，当遇到数组时会调用 createSheet创建新的页面。</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> jsonObject    JSON对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> savePath      Excel保存路径</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> excelName     Excel名称</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">toExcelByJSONObject</span><span class="hljs-params">(JSONObject jsonObject,  String savePath, String excelName)</span>&#123;<br>        <span class="hljs-keyword">try</span>(<span class="hljs-type">XSSFWorkbook</span> <span class="hljs-variable">workbook</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">XSSFWorkbook</span>())&#123;<br>            <span class="hljs-comment">// 获取当前的Sheet</span><br>            <span class="hljs-type">XSSFSheet</span> <span class="hljs-variable">sheet</span> <span class="hljs-operator">=</span> workbook.createSheet(<span class="hljs-string">&quot;sheet&quot;</span>);<br>            <span class="hljs-comment">// 获取第一行</span><br>            <span class="hljs-type">XSSFRow</span> <span class="hljs-variable">firstRow</span> <span class="hljs-operator">=</span> sheet.createRow(<span class="hljs-number">0</span>);<br>            <span class="hljs-comment">// 记录Key所在的列</span><br>            Map&lt;String, Integer&gt; map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>            <span class="hljs-comment">// 记录列数</span><br>            <span class="hljs-type">int</span> <span class="hljs-variable">cellCount</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>            <span class="hljs-comment">// 遍历 JSON的key</span><br>            <span class="hljs-type">XSSFRow</span> <span class="hljs-variable">currentRow</span> <span class="hljs-operator">=</span> sheet.createRow(<span class="hljs-number">1</span>);<br>            <span class="hljs-keyword">for</span> (String key : jsonObject.keySet()) &#123;<br>                <span class="hljs-comment">// 先处理列</span><br>                <span class="hljs-keyword">if</span>(!map.containsKey(key))&#123;  <span class="hljs-comment">// 当列不存在则添加</span><br>                    map.put(key, cellCount);<br>                    <span class="hljs-type">XSSFCell</span> <span class="hljs-variable">cell</span> <span class="hljs-operator">=</span> firstRow.createCell(cellCount++);<br>                    cell.setCellValue(key);<br>                &#125;<br>                <span class="hljs-type">XSSFCell</span> <span class="hljs-variable">currentCell</span> <span class="hljs-operator">=</span> currentRow.createCell(map.get(key));<br>                <span class="hljs-type">String</span> <span class="hljs-variable">jsonStr</span> <span class="hljs-operator">=</span> jsonObject.getString(key);<br>                <span class="hljs-comment">// 如果 Value为数组 则创建新的 Sheet</span><br>                <span class="hljs-keyword">if</span>(JSON.isValidArray(jsonStr))&#123;<br>                    <span class="hljs-type">String</span> <span class="hljs-variable">subSheetName</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Sheet-&quot;</span> + key;<br>                    createSubSheet(<span class="hljs-number">1</span>, workbook, subSheetName, jsonObject.getJSONArray(key));<br>                    currentCell.setCellValue(subSheetName);<br>                &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(JSON.isValidObject(jsonStr))&#123; <span class="hljs-comment">// 如果当前 value 仍然是一个JSON对象</span><br>                    <span class="hljs-type">String</span> <span class="hljs-variable">subSheetName</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Sheet-&quot;</span> + key;<br>                    createSubSheet(<span class="hljs-number">1</span>, workbook, subSheetName, jsonObject.getJSONObject(key));<br>                    currentCell.setCellValue(subSheetName);<br>                &#125;<br>                <span class="hljs-keyword">else</span> &#123;<br>                    <span class="hljs-comment">// 特殊处理空值</span><br>                    <span class="hljs-keyword">if</span>(StringUtils.isEmpty(jsonStr))<br>                        currentCell.setCellValue(<span class="hljs-string">&quot;null&quot;</span>);<br>                    <span class="hljs-keyword">else</span><br>                        currentCell.setCellValue(jsonStr);<br>                &#125;<br>            &#125;<br>            save(workbook, savePath, excelName);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException ex) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(ex);<br>        &#125;<br>    &#125;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 将 Excel对象保存到本地</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> workbook Excel对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> path Excel文件路径</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> excelName excel名称</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">save</span><span class="hljs-params">(Workbook workbook, String path, String excelName)</span>&#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-type">FileOutputStream</span> <span class="hljs-variable">fileOutputStream</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">FileOutputStream</span>(path +<span class="hljs-string">&quot;/&quot;</span> + excelName +<span class="hljs-string">&quot;.xlsx&quot;</span>);<br>            workbook.write(fileOutputStream);<br>            fileOutputStream.close();<br>            System.out.println(<span class="hljs-string">&quot;保存完毕. 保存位置为[ &quot;</span> + path + <span class="hljs-string">&quot;/&quot;</span> + excelName + <span class="hljs-string">&quot; ]&quot;</span>);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">jsonPath</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;C:\\Users\\unirithe\\IdeaProjects\\poi_demo\\src\\main\\resources\\data.json&quot;</span>;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">savePath</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;C:\\Users\\unirithe\\Desktop&quot;</span>;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">excelName</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;demo&quot;</span>;<br>        <span class="hljs-comment">// 测试1</span><br>        toExcelByLocalJSONFile(jsonPath, savePath, excelName + <span class="hljs-string">&quot;1&quot;</span>);<br>        <span class="hljs-type">String</span> <span class="hljs-variable">jsonStr</span> <span class="hljs-operator">=</span> readJSONFile(jsonPath);<br>        <span class="hljs-type">JSONObject</span> <span class="hljs-variable">jsonObject</span> <span class="hljs-operator">=</span> JSONObject.parseObject(jsonStr);<br>        <span class="hljs-type">Object</span> <span class="hljs-variable">object</span> <span class="hljs-operator">=</span> JSON.parse(jsonStr);<br>        <span class="hljs-comment">// 测试2</span><br>        toExcelByString(jsonStr, savePath, excelName + <span class="hljs-string">&quot;2&quot;</span>);<br>        <span class="hljs-comment">// 测试3</span><br>        toExcelByObject(object, savePath, excelName + <span class="hljs-string">&quot;3&quot;</span>);<br>        <span class="hljs-comment">// 测试4</span><br>        toExcelByJSONObject(jsonObject, savePath, excelName + <span class="hljs-string">&quot;4&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>测试的JSON数据：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><code class="hljs js">&#123;<br>  <span class="hljs-string">&quot;班级A&quot;</span>:[<br>    &#123;<br>      <span class="hljs-string">&quot;学号&quot;</span>:<span class="hljs-string">&quot;A01&quot;</span>,<br>      <span class="hljs-string">&quot;语文&quot;</span>:[<br>        &#123;<br>          <span class="hljs-string">&quot;文章&quot;</span>:<span class="hljs-string">&quot;课文1&quot;</span>,<br>          <span class="hljs-string">&quot;作者&quot;</span>:<span class="hljs-string">&quot;李白&quot;</span><br>        &#125;,<br>        &#123;<br>          <span class="hljs-string">&quot;文章&quot;</span>:<span class="hljs-string">&quot;课文2&quot;</span>,<br>          <span class="hljs-string">&quot;作者&quot;</span>:<span class="hljs-string">&quot;小李&quot;</span><br>        &#125;,<br>        &#123;<br>          <span class="hljs-string">&quot;文章&quot;</span>:<span class="hljs-string">&quot;课文2&quot;</span>,<br>          <span class="hljs-string">&quot;作者&quot;</span>: <span class="hljs-string">&quot;小明&quot;</span><br>        &#125;<br>      ],<br>      <span class="hljs-string">&quot;数学&quot;</span>:<span class="hljs-string">&quot;130&quot;</span><br>    &#125;,<br>    &#123;<br>      <span class="hljs-string">&quot;学号&quot;</span>:<span class="hljs-string">&quot;A02&quot;</span>,<br>      <span class="hljs-string">&quot;语文&quot;</span>:<span class="hljs-string">&quot;130&quot;</span>,<br>      <span class="hljs-string">&quot;数学&quot;</span>:<span class="hljs-string">&quot;135&quot;</span><br>    &#125;<br>  ],<br>  <span class="hljs-string">&quot;班级B&quot;</span>:[<br>    &#123;<br>      <span class="hljs-string">&quot;学号&quot;</span>:<span class="hljs-string">&quot;B01&quot;</span>,<br>      <span class="hljs-string">&quot;语文&quot;</span>:<span class="hljs-string">&quot;128&quot;</span>,<br>      <span class="hljs-string">&quot;数学&quot;</span>:<span class="hljs-string">&quot;135&quot;</span><br>    &#125;,<br>    &#123;<br>      <span class="hljs-string">&quot;学号&quot;</span>:<span class="hljs-string">&quot;B02&quot;</span>,<br>      <span class="hljs-string">&quot;语文&quot;</span>:<span class="hljs-string">&quot;133&quot;</span>,<br>      <span class="hljs-string">&quot;数学&quot;</span>:<span class="hljs-string">&quot;140&quot;</span><br>    &#125;<br>  ]<br>&#125;<br></code></pre></td></tr></table></figure>

<p>测试结果如下，这里保存的demo1、demo2、demo3和demo4结果是一致的，主要是为了测试不同方法的正确性。</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/0be9bdf37258466bafb436511895d3a3.png#">https://img-blog.csdnimg.cn/0be9bdf37258466bafb436511895d3a3.png#</a> &#x3D;80%x)<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/70a0946f221241d18ee2b9d0f04df732.png#">https://img-blog.csdnimg.cn/70a0946f221241d18ee2b9d0f04df732.png#</a> &#x3D;80%x)<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/e990e9eb9a29401880852476eac9b269.png#">https://img-blog.csdnimg.cn/e990e9eb9a29401880852476eac9b269.png#</a> &#x3D;80%x)<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/4f559f67b3ca4ca4ba8490e92184f052.png#">https://img-blog.csdnimg.cn/4f559f67b3ca4ca4ba8490e92184f052.png#</a> &#x3D;80%x)</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/JavaSE/" class="category-chain-item">JavaSE</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Java/">#Java</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Java实现将JSON文件导出到Excel</div>
      <div>http://example.com/2022/07/06/Java实现将JSON文件导出到Excel/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年7月6日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/07/09/Quartz%20%E7%AC%94%E8%AE%B01%20%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6%E6%A1%86%E6%9E%B6%20_%20%E6%A0%B8%E5%BF%83%E7%BB%84%E4%BB%B6%20Job%E3%80%81Trigger%20%E5%92%8C%20Scheduler%20_%20%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B%20_%20%E7%9B%91%E5%90%AC%E5%99%A8/" title="Quartz 笔记1 任务调度框架 _ 核心组件 Job、Trigger 和 Scheduler _ 入门案例 _ 监听器">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Quartz 笔记1 任务调度框架 _ 核心组件 Job、Trigger 和 Scheduler _ 入门案例 _ 监听器</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/07/04/Swagger%20%E7%AC%94%E8%AE%B0%20_%20Swagger%20%E7%AE%80%E4%BB%8B%20_%20Springfox%20%E7%AE%80%E4%BB%8B%20_%20Springfox%202.9.2%20%E5%B8%B8%E7%94%A8%E6%B3%A8%E8%A7%A3%20_%20Spring%20Boot%20%E6%95%B4%E5%90%88%20Swagger2%20%E6%A1%88%E4%BE%8B/" title="Swagger 笔记 _ Swagger 简介 _ Springfox 简介 _ Springfox 2.9.2 常用注解 _ Spring Boot 整合 Swagger2 案例">
                        <span class="hidden-mobile">Swagger 笔记 _ Swagger 简介 _ Springfox 简介 _ Springfox 2.9.2 常用注解 _ Spring Boot 整合 Swagger2 案例</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
